//
//  BankCardsViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/5.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "BankCardsViewModel.h"
#import "BankCardsTableViewCell.h"
#import "BankFooterView.h"
#import "BankCardDetailTableViewCell.h"
#import "BankHeaderView.h"

@interface BankCardsViewModel ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
}

@end

@implementation BankCardsViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        tableView.sectionFooterHeight = 0.0;
        tableView.sectionHeaderHeight = 0.0;
        [tableView registerClass:[BankFooterView class] forHeaderFooterViewReuseIdentifier:@"BankFooterView"];
        [tableView registerClass:[BankHeaderView class] forHeaderFooterViewReuseIdentifier:@"BankHeaderView"];
        [superView addSubview:tableView];
        selfTableView = tableView;
        if (self.bankCardType == BankCardsType_None) {
        }        
    }
    return self;
}

- (void)setBankCardType:(BankCardsType)bankCardType
{
    _bankCardType = bankCardType;
    if (bankCardType == BankCardsType_None) {
        selfTableView.backgroundColor = [UIColor clearColor];
    } else {
        selfTableView.backgroundColor = [UIColor whiteColor];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.bankCardType == BankCardsType_None) {
        return 123;
    }
    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    BankHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"BankHeaderView"];
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    BankFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"BankFooterView"];
    kWeakSelf(self);
    footer.BlockApplyWithDraw = ^(GLGWButton * _Nullable applyWithDraw) {
        [weakself applyWithDraw:applyWithDraw];
    };
    return footer;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.bankCardType == BankCardsType_None) {
        return 1;
    }
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return kHeightProportion_In_iPhoneTriploid(44);
    } else {
        return kHeightProportion_In_iPhoneTriploid(120);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (indexPath.row == 0) {
        BankCardsTableViewCell *banCell = [BankCardsTableViewCell cellWithTableView:tableView];
        cell = banCell;
    } else {
        BankCardDetailTableViewCell *detailCell = [BankCardDetailTableViewCell cellWithTableView:tableView];
        cell = detailCell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if (self.BlockChooseBank) {
            self.BlockChooseBank();
        }
    }
}

- (void)applyWithDraw:(GLGWButton *)sender
{
    if (self.BlockApplyWithDraw) {
        self.BlockApplyWithDraw(sender);
    }
}

- (void)reloadUI
{
    [selfTableView reloadData];
}

@end
